package com.wx.mapper;


import com.wx.dto.BorrowAgreeDTO;
import com.wx.dto.BorrowPageQueryDTO;
import com.wx.entity.Borrow;
import com.wx.vo.BorrowPageQueryVO;
import com.wx.vo.BorrowTimeVO;
import com.wx.vo.BorrowVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;


@Mapper
public interface BorrowMapper {

    @Insert("insert into " +
            "borrow(book_id, borrow_time, borrow_id, return_time,is_agree,is_return,is_finish) " +
            "values(#{bookId}, #{borrowTime}, #{borrowId}, #{returnTime},#{isAgree},#{isReturn},#{isFinish})"
    )
    Integer save(Borrow borrow);



    // 查询借阅信息
    //@Select("select * from borrow where borrow_id = #{id}")
    @Select("select b.*, a.book_name,a.image from borrow b left outer join book a on b.book_id = a.book_no where is_return = 0 and is_agree = 1 and b.borrow_id = #{id}")
    List<BorrowVO> getBorrowById(Integer id);

    @Select("select b.*, a.book_name,a.image from borrow b left outer join book a on b.book_id = a.book_no where is_finish = 0 and b.borrow_id = #{id}")
    List<BorrowVO> getBorrowByIdR(Integer id);

    /*根据借阅id查询信息*/
    @Select("select borrow_time,return_time from borrow where id = #{id}")
    BorrowTimeVO getBorrowInfo(Integer id);




    /*设置isAgree为1*/
    @Update("update borrow set is_agree = #{isAgree},auditer = #{auditer} where id = #{id}")
    void setIsAgree(BorrowAgreeDTO borrowAgreeDTO);

    /*设置isReturn为1*/
    @Update("update borrow set is_return = 2 where id = #{id}")
    void updateReturn(Integer id);

    /*通过id查询book_id*/
    @Select("select book_id from borrow where id = #{id}")
    Integer selectBookId(Integer id);

    /*通过id查询borrow_id*/
    @Select("select borrow_id from borrow where id = #{id}")
    Integer selectBorrowId(Integer id);

    /*条件查询借阅数量*/
    Integer queryBorrowkCount(BorrowPageQueryDTO borrowPageQueryDTO);

    /*条件查询借阅历史*/
    List<BorrowPageQueryVO> pageQuery(BorrowPageQueryDTO borrowPageQueryDTO);

    /*根据id更新还书信息*/
    void updateBorrowInfo(Borrow borrow);

    /*同意还书*/
    @Update("update borrow set is_return = #{isReturn},auditer=#{auditer},is_finish=1 where id = #{id}")
    void setIsReturn(BorrowAgreeDTO borrowAgreeDTO);
}
